#!/usr/bin/env python
#coding=utf-8

from pysqlite2 import dbapi2 as sqlite

def create_table_exchangedays(con):
    sql = "create table exchangedays (id integer primary key,\
                                      code text not null,\
                                      date text not null,\
                                      pre_day_id integer default null);"
    c = con.cursor()
    c.execute(sql)
    
    con.commit()
    c.close()
    
    
def list2db(con, code, days):
    c = con.cursor()
    pre_day_id = None
    for d in days:
        if not pre_day_id:
            sql = "insert into exchangedays(code, date) values(%s, '%s')"%(code, d)
        else:
            sql = "insert into exchangedays(code, date, pre_day_id) values(%s,'%s',%i)"%(code,d,pre_day_id)
        c.execute(sql)
        con.commit()
        c.execute('select last_insert_rowid()')
        pre_day_id = c.fetchone()[0]
    con.commit()
    c.close()

        